package a08_回溯算法;

import java.util.*;

/**
 * <p>
 * a14_递增子序列复习1
 * </p>
 *
 * @author flyduck
 * @since 2025-01-05
 */
public class a14_递增子序列复习1 {
    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        traversal(result, path, nums, 0);
        return result;
    }

    private void traversal(List<List<Integer>> result,
                           LinkedList<Integer> path,
                           int[] nums,
                           int startIdx){
        if(path.size() >= 2){
            result.add(new ArrayList<>(path));
        }
        if(startIdx == nums.length){
            return;
        }
        Set<Integer> set = new HashSet<>();
        for (int i = startIdx; i < nums.length; i++) {
            if(!path.isEmpty() && nums[i] < path.get(path.size() - 1)){
                continue;
            }
            if(set.contains(nums[i])){
                continue;
            }
            path.add(nums[i]);
            set.add(nums[i]);
            traversal(result, path, nums, i + 1);
            path.removeLast();
        }
    }
}
